package com.example.project.dao.impl;

import com.example.project.dao.LotteryExtDao;
import com.example.project.dao.base.impl.BaseDaoImpl;
import com.example.project.model.bo.LotteryBo;
import com.example.project.model.bo.PagingParam;
import com.example.project.model.jooq.Tables;
import org.jooq.SelectOffsetStep;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Administrator on 2017/12/10.
 */
@Component
public class LotteryExtDaoImpl extends BaseDaoImpl implements LotteryExtDao {
    /**
     * 查询首页列表
     * @return
     */
    @Override
    public List<LotteryBo> loadLotteryList(PagingParam pagingParam) {
        List<LotteryBo> lotteryBoList = null;
        try {
            lotteryBoList =this.getDslContext()
                    .select(Tables.LOTTERY.fields())
                    .from(Tables.LOTTERY)
                    .where(Tables.LOTTERY.INPUTFLAG.eq("BONUS"))
                    .orderBy(Tables.LOTTERY.ISSUE_NO.desc())
                    .offset(pagingParam.getOffset())
                    .limit(pagingParam.getPrepage())
                    .fetchInto(LotteryBo.class);
        }catch (Exception e){

        }
        return lotteryBoList;
    }

    @Override
    public List<LotteryBo> loadInputLotteryList(PagingParam pagingParam) {
        List<LotteryBo> lotteryBoList = null;
        try {
            lotteryBoList =this.getDslContext()
                    .select(Tables.LOTTERY.fields())
                    .select(Tables.LOTTERY.ISSUE_NO.countOver().as("totalLine"))
                    .from(Tables.LOTTERY)
                    .where(Tables.LOTTERY.INPUTFLAG.eq("INPUT"))
                    .orderBy(Tables.LOTTERY.ISSUE_NO.asc())
                    .offset(pagingParam.getOffset())
                    .limit(pagingParam.getPrepage())
                    .fetchInto(LotteryBo.class);
        }catch (Exception e){

        }
        return lotteryBoList;
    }

    /**
     * 查询总记录数
     * @return
     */
    @Override
    public int loadLotteryListCount() {
        int count = 0 ;
        try {
            count =this.getDslContext()
                    .selectCount()
                    .from(Tables.LOTTERY)
                    .where(Tables.LOTTERY.INPUTFLAG.eq("BONUS"))
                    .fetchOneInto(Integer.class).intValue();
        }catch (Exception e){

        }
        return count;
    }


    /**
     * 查询最新的记录数目
     * @return
     */
    @Override
    public LotteryBo loadNewestLotteryPo() {
        LotteryBo lotteryBo = null;
        try {
            lotteryBo  =this.getDslContext()
                    .select(Tables.LOTTERY.fields())
                    .from(Tables.LOTTERY)
                    .where(Tables.LOTTERY.INPUTFLAG.eq("BONUS"))
                    .orderBy(Tables.LOTTERY.ISSUE_NO.desc())
                    .limit(1).fetchOneInto(LotteryBo.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return lotteryBo;
    }

    @Override
    public LotteryBo loadAllNewestLotteryPo() {
        LotteryBo lotteryBo = null;
        try {
            lotteryBo  =this.getDslContext()
                    .select(Tables.LOTTERY.fields())
                    .from(Tables.LOTTERY)
                    .orderBy(Tables.LOTTERY.ISSUE_NO.desc())
                    .limit(1).fetchOneInto(LotteryBo.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return lotteryBo;
    }

    @Override
    public LotteryBo loadInputLastLotteryPo() {
        LotteryBo lotteryBo = null;
        try {
            lotteryBo  =this.getDslContext()
                    .select(Tables.LOTTERY.fields())
                    .from(Tables.LOTTERY)
                    .where(Tables.LOTTERY.INPUTFLAG.eq("INPUT"))
                    .orderBy(Tables.LOTTERY.ISSUE_NO.asc())
                    .limit(1).fetchOneInto(LotteryBo.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return lotteryBo;
    }


    /**
     * 查询首页列表
     * @return
     */
    @Override
    public List<LotteryBo> loadLotteryDownList() {
        List<LotteryBo> lotteryBoList = null;
        try {
            SelectOffsetStep selectConditionStep  =this.getDslContext()
                    .select(Tables.LOTTERY.fields())
                    .from(Tables.LOTTERY)
                    .where(Tables.LOTTERY.LOTTERY_TIME.le(System.currentTimeMillis()))
                    .and(Tables.LOTTERY.INPUTFLAG.eq("BONUS"))
                    .orderBy(Tables.LOTTERY.ISSUE_NO.desc())
                    .limit(10);
            lotteryBoList = selectConditionStep.fetchInto(LotteryBo.class);
        }catch (Exception e){

        }
        return lotteryBoList;
    }

    @Override
    public LotteryBo loadLotteryBo(long issueNo) {
        LotteryBo lotteryBo = null;
        try {
            lotteryBo =this.getDslContext()
                    .select(Tables.LOTTERY.fields())
                    .from(Tables.LOTTERY)
                    .where(Tables.LOTTERY.ISSUE_NO.eq(issueNo))
                    .fetchOneInto(LotteryBo.class);
        }catch (Exception e){

        }
        return lotteryBo;
    }
}
